package main

import (
	"backend/dao/mysql"
	"backend/dao/redis"
	"backend/logger"
	"backend/routers"
	"backend/settings"
	"fmt"
)

func main() {
	// 加载配置文件
	if err := settings.Init(); err != nil {
		fmt.Printf("load config failed, err:%v\n", err)
		return
	}
	// 初始化第三方日志库
	if err := logger.Init(settings.Conf.LogConfig, settings.Conf.Mode); err != nil {
		fmt.Printf("init logger failed, err:%v\n", err)
		return
	}
	// 创建mysql连接对象
	if err := mysql.Init(settings.Conf.MySQLConfig); err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer mysql.Close() // 程序退出时关闭mysql数据库连接
	// 测试查询
	operateHistoryList, err := mysql.GetOperateHistoryList()
	for _, operateHistory := range operateHistoryList {
		fmt.Printf("operateHistory:%#v, err:%v\n", operateHistory, err)
	}
	// operateHistory:&models.OperateHistory{ID:0x15, CreateTime:time.Date(2022, time.July, 11, 16, 2, 30, 835143000, time.Local), UpdateTime:time.Date(2022, time.July, 11, 16, 2, 30, 835171000, time.Local), PnodeId:(*uint64)(nil), Content:"2022-07-11 16:02:30 由 缪克拉 提交验收。"}, err:<nil>
	// ...

	// 创建redis连接
	if err := redis.Init(settings.Conf.RedisConfig); err != nil {
		fmt.Printf("init redis failed, err:%v\n", err)
		return
	}

	//proxyList := redis.GetHttpProxy()
	//for _, proxy := range proxyList {
	//	fmt.Printf("proxy:%s\n", proxy)
	//}
	// proxy:http://220.179.219.187:4213
	// proxy:http://122.246.95.250:4245
	// proxy:http://114.225.253.148:4263
	// ...
	defer redis.Close()
	// 注册路由
	r := routers.SetupRouter()
	err = r.Run(fmt.Sprintf(":%d", settings.Conf.Port))
	if err != nil {
		fmt.Printf("run server failed, err:%v\n", err)
		return
	}
}
